package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //添加检查组
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        //获取检查组id
        Integer checkGroupId = checkGroup.getId();
        //操作t_checkgroup_checkitem表,建立两者多对多关系
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }


    //检查组分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //获取当前页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页显示记录数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);

        long total = page.getTotal();
        List<CheckGroup> result = page.getResult();

        return new PageResult(total, result);
    }

    //通过检查组id查询关联的检查项id集合
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {

        return checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);

    }

    //更新检查组
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //获取检查组id
        Integer checkGroupId = checkGroup.getId();

        //根据检查组id删除中间表的检查项id
        checkGroupDao.deleteCheckItemIdsByCheckGroupId(checkGroupId);

        //操作t_checkgroup_checkitem表,建立两者多对多关系
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);

        //更新检查组基本信息
        checkGroupDao.edit(checkGroup);

    }

    //删除功能
    /*根据检查组id,判断是否关联套餐,若关联不能删除,
    不关联要先删除检查项和检查组的关联表关系,再删除检查组的基本信息*/
    public void delete(Integer checkGroupId) {
        int count = checkGroupDao.findCountSetmealIdByCheckGroupId(checkGroupId);

        if (count>0){
            throw new RuntimeException("当前检查组被引用,暂不能删除");
        }else {
            //先删除组与项中间表的关联关系
            checkGroupDao.deleteCheckItemIdsByCheckGroupId(checkGroupId);

            //再删除检查组的基本信息
            checkGroupDao.deleteCheckGroupById(checkGroupId);
        }

    }


    //建立检查组和检查项的多对多关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }

    }
    //查询所有检查组
    public List<CheckGroup> findAllCheckGroup() {

        return checkGroupDao.findAllCheckGroup();
    }


}
